"""
@author: yinjx
@file: user.py
@time: 2023/8/3 19:36
"""
from models import db
from werkzeug.security import generate_password_hash
import datetime

#API授权表的模型
#多对多的关系
app_permission = db.Table("app_permission",
                          db.Column("api_id",db.ForeignKey("api_token.id")),
                          db.Column("permission_id",db.ForeignKey("api_permission.id"))
                          )
# api_token表
#存放的是授权密钥，以及授权id
class ApiToken(db.Model):
    __tablename__ = "api_token"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    appid = db.Column(db.String(128), nullable=False)
    secretkey = db.Column(db.String(128), nullable=False)
    #通过中间表去创建多对多的关系
    manage = db.relationship("ApiPermission", secondary=app_permission, backref="token")

#存放的是授权的url
class ApiPermission(db.Model):
    __tablename__ = "api_permission"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    url = db.Column(db.String(128), nullable=False)
    method_type = db.Column(db.String(128), nullable=False)


# 用户表
class User(db.Model):
    __tablename__ = "user"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(128), nullable = False)
    _password = db.Column("password", db.String(128), nullable=False)
    # role = db.Column(db.Integer, default=0)
    # add_time = db.Column(db.DateTime, default = datetime.datetime.now)
    avatar = db.Column(db.String(256), default=0)
    created_at = db.Column(db.DateTime, default=datetime.datetime.now)
    #property python内置装饰器  属性包装装饰器
    #作用：把方法当做属性一样使用
    #定义属性之前做一些检测、转换

    @property   #自动根据函数名生成两个装饰器  --》password.setter password.deleter
    def password(self):
        return self._password
    #user.password()   --> print(user.password)

    ##定义属性之前做一些检测、转换
    #user.password = xxxx
    @password.setter
    def password(self, value):
        self._password = generate_password_hash(value)

    @classmethod  #类方法  第一个参数代表类本身
    def create_user(cls, username, password):
        user = cls()   #创建实例对象
        print(username, password)
        user.username = username
        user.password = password #调用password.setter装饰的函数,强制性要求存hash值
        #user._password = generate_password_hash(password)
        #user._password = password
        db.session.add(user)
        db.session.commit()


# 书籍表（Book）
class Book(db.Model):
    __tablename__ = 'book'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(256))
    author = db.Column(db.String(256))
    cover = db.Column(db.String(256))
    description = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=datetime.datetime.now)




# 评论表（Comment）
class Comment(db.Model):
    __tablename__ = 'comment'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    book_id = db.Column(db.Integer, db.ForeignKey('book.id'))
    content = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=datetime.datetime.now)

    user = db.relationship('User', backref=db.backref('comments', lazy=True))
    book = db.relationship('Book', backref=db.backref('comments', lazy=True))

# 点赞表（Like）
class Like(db.Model):
    __tablename__ = 'like'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    comment_id = db.Column(db.Integer, db.ForeignKey('comment.id'))

    user = db.relationship('User', backref=db.backref('likes', lazy=True))
    comment = db.relationship('Comment', backref=db.backref('likes', lazy=True))

# 分类表（Category）
class Category(db.Model):
    __tablename__ = 'category'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(256))

# 书籍分类表（Book_Category）
class BookCategory(db.Model):
    __tablename__ = 'book_category'
    book_id = db.Column(db.Integer, db.ForeignKey('book.id'), primary_key=True)
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'), primary_key=True)

    book = db.relationship('Book', backref=db.backref('categories', lazy=True))
    category = db.relationship('Category', backref=db.backref('books', lazy=True))